package com.auto.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auto.entity.Art;
import com.auto.rep.ArtRep;
import com.auto.util.LOG;
import com.google.common.collect.Lists;

@Controller
public class ArtController {

	@Autowired
	private ArtRep artRep;

	@RequestMapping(value = "art", method = RequestMethod.GET)
	@ResponseBody
	public List<Art> getArts() {
		return Lists.newArrayList(artRep.findAll());
	}

	@RequestMapping(value = "art/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Art getArt(@PathVariable("id") Long id) {
		return artRep.findOne(id);
	}

	@RequestMapping(value = "art/{id}", method = RequestMethod.POST)
	@ResponseBody
	public void updateArt(@PathVariable("id") Long id, Art art) {
		LOG.info("更新文章:{}", art);
		art.setId(id);
		artRep.save(art);
	}

	@RequestMapping(value = "art/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteArt(@PathVariable("id") Long id) {
		artRep.delete(id);
	}

	@RequestMapping(value = "art", method = RequestMethod.POST)
	@ResponseBody
	public Art addArt(Art art) {
		LOG.info("更新文章:{}", art);
		return artRep.save(art);
	}
}
